﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace AppManager
{
    public partial class FrmNewList : Form
    {
        public FrmNewList()
        {
            InitializeComponent();
        }

        private void btnSave_Click(object sender, EventArgs e)
        {
            MetaListPage listPage = new MetaListPage()
            {
                CardPage = txtName.Text,
                FilterCondition = "",
                OrderCondition = "",
                RecordCount = 25,
                Name = txtName.Text
            };

            listPage.Fields = new List<MetaFormField>();
            foreach (DataGridViewRow row in GridFields.Rows)
            {
                if (row.Cells[0].Value != null)
                {
                    listPage.Fields.Add(new MetaFormField()
                    {
                        Name = row.Cells[0].Value.ToString()
                    });
                }
            }

            string formXml = Utility.SerializeObject(listPage);
            AppObject createAppObject = new AppObject()
            {
                CreatedDate = DateTime.Now,
                Metadata = formXml,
                ModuleID = Convert.ToInt32(txtModuleID.Text),
                Name = txtName.Text.ToUpper().Trim(),
                ObjectType = "LIST"
            };

            using (CRMDataContext dataContext = new CRMDataContext())
            {
                dataContext.AppObjects.InsertOnSubmit(createAppObject);
                dataContext.SubmitChanges();
            }
        }
    }
}
